package com.justech.eat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justech.common.util.MesAssert;
import com.justech.common.util.http.Hkv;
import com.justech.common.util.http.HttpClientUtil;
import com.justech.eat.entity.Config;
import com.justech.eat.entity.User;
import com.justech.eat.service.ConfigService;
import com.justech.eat.service.UserService;
import com.justech.eat.service.component.UserDataSupplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: eat-service
 * @description: 默认的用户信息供应商
 * @author: feifei.li
 * @create: 2020-06-10 11:39
 **/
@Component
public class HrServiceUserDataSupplier implements UserDataSupplier {

    @Autowired
    UserService userService;
    @Autowired
    ConfigService configService;

    private static String userHrUrl;
    private static String userHrUrlToken;
    private static String param;
    private static Integer totalPages;

    @Override
    public List<User> getUserData() {
        userHrUrl = configService.get("user_hr_url").getValue();
        userHrUrlToken = configService.get("user_hr_url_token").getValue();
        param = configService.get("user_hr_url_param").getValue();
        /*从hr系统中获取数据*/
        return parseUserList(request());
    }

    private JSONArray request() {
        String paramJson = parameter(1);
        JSONArray jsonArray = new JSONArray();
        JSONArray page = getPage(paramJson);
        jsonArray.addAll(page);
        if (totalPages != null) {
            for (int i = 2; i <= totalPages; i++) {
                JSONArray array = getPage(parameter(i));
                jsonArray.addAll(array);
            }
        }
        totalPages = null;
        return jsonArray;
    }

    private JSONArray getPage(String paramJson) {
        String json = HttpClientUtil.doPostJson(userHrUrl,
                paramJson, new Hkv("ThirdPartyAccessToken", userHrUrlToken));
        if (json != null) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (jsonObject.getBoolean("IsSuccess")) {
                /*提取总页数*/
                totalPages = jsonObject.getInteger("TotalPages");
                return jsonObject.getJSONArray("DataRows");
            } else {
                MesAssert.throwBizException("请求hr失败：" + jsonObject.toString());
            }
            return JSONObject.parseArray(json);
        } else {
            MesAssert.throwBizException("请求hr失败," + userHrUrl + "\ttoken:" + userHrUrlToken);
        }
        return null;
    }

    private List<User> parseUserList(JSONArray jsonArray) {
        ArrayList<User> users = null;
        if (jsonArray.size() > 0) {
            users = new ArrayList<>(jsonArray.size());
            for (Object o : jsonArray) {
                JSONObject jsonObject = JSONObject.parseObject(o.toString());
                User user = new User();
                user.setPin(jsonObject.getString("_kqcode"));
                user.setDeptCode(jsonObject.getJSONObject("Dept").getString("DeptCode"));
                user.setDeptName(jsonObject.getJSONObject("Dept").getString("DeptName"));
                user.setJobNumber(jsonObject.getString("EmpCode"));
                user.setUserName(jsonObject.getString("EmpName"));
                user.setMobile(jsonObject.getString("PhoneNumber"));
                users.add(user);
            }
        }
        return users;
    }

    private String parameter(int page) {
        JSONObject jsonObject = JSONObject.parseObject(param);
        jsonObject.put("currentPage", page);
        return jsonObject.toJSONString();
    }
}
